package com.commerce.user.controller;

import com.commerce.common.entity.Addresses;
import com.commerce.common.utils.PageResult;
import com.commerce.common.utils.Result;
import com.commerce.user.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/addresses")
@CrossOrigin
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取用户地址列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<Addresses>> getUserAddresses(@PathVariable Long userId) {
        try {
            List<Addresses> addresses = addressService.getUserAddresses(userId);
            return Result.success(addresses);
        } catch (Exception e) {
            log.error("获取用户地址列表失败", e);
            return Result.error("获取用户地址列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取地址详情
     */
    @GetMapping("/{id}")
    public Result<Addresses> getAddressById(@PathVariable Long id) {
        try {
            Addresses address = addressService.getById(id);
            if (address == null) {
                return Result.error("地址不存在");
            }
            return Result.success(address);
        } catch (Exception e) {
            log.error("获取地址详情失败", e);
            return Result.error("获取地址详情失败：" + e.getMessage());
        }
    }

    /**
     * 添加地址
     */
    @PostMapping
    public Result<Boolean> addAddress(@RequestBody Addresses address) {
        try {
            boolean success = addressService.addAddress(address);
            return Result.success(success);
        } catch (Exception e) {
            log.error("添加地址失败", e);
            return Result.error("添加地址失败：" + e.getMessage());
        }
    }

    /**
     * 更新地址
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateAddress(@PathVariable Long id, @RequestBody Addresses address) {
        try {
            address.setId(id);
            boolean success = addressService.updateAddress(address);
            return Result.success(success);
        } catch (Exception e) {
            log.error("更新地址失败", e);
            return Result.error("更新地址失败：" + e.getMessage());
        }
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAddress(@PathVariable Long id) {
        try {
            boolean success = addressService.removeById(id);
            return Result.success(success);
        } catch (Exception e) {
            log.error("删除地址失败", e);
            return Result.error("删除地址失败：" + e.getMessage());
        }
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/{id}/default")
    public Result<Boolean> setDefaultAddress(@PathVariable Long id, @RequestParam Long userId) {
        try {
            boolean success = addressService.setDefaultAddress(id, userId);
            return Result.success(success);
        } catch (Exception e) {
            log.error("设置默认地址失败", e);
            return Result.error("设置默认地址失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户默认地址
     */
    @GetMapping("/user/{userId}/default")
    public Result<Addresses> getUserDefaultAddress(@PathVariable Long userId) {
        try {
            Addresses address = addressService.getUserDefaultAddress(userId);
            return Result.success(address);
        } catch (Exception e) {
            log.error("获取用户默认地址失败", e);
            return Result.error("获取用户默认地址失败：" + e.getMessage());
        }
    }
}
